﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using Net.Chirash.Twitter.TwitterClient;
using Net.Chirash.Twitter.TwitterDatabase;

namespace Net.Chirash.Twitter.TwitterClient.DBSyncronize
{
    public class TwitterPushTask<T>
        : ObservableTaskBase<IEnumerable<T>>
    {

        public TwitterPushTask(string taskname)
            : base(taskname)
        {
        }

        public override void DoTask(object arg)
        {
            var res = arg as IEnumerable<T>;
            if (res == null)
                return;
            
            try
            {
                this.NotifyRefreshStart();
                try
                {
                    this.NotifyNext(res);
                }
                catch (Exception ex)
                {
                    this.NotifyError(ex);
                }
            }
            finally
            {
                this.NotifyRefreshEnd();
            }
        }
    }

    public class TwitterPullUpdateTask<T>
        : ObservableTaskBase<IEnumerable<T>>
        where T : TwitterData
    {
        public Func<ITwitterCommand, IEnumerable<T>> GettingFunc { get; private set; }
        public Action<TwitterCaching, IEnumerable<T>> ApplyingFunc { get; private set; }
        public ITwitterCommand SourceCommand { get; private set; }
        public TwitterCaching TargetCaching { get; private set; }

        public TwitterPullUpdateTask(string taskname, ITwitterCommand srccmd, TwitterCaching caching, Func<ITwitterCommand, IEnumerable<T>> getting, Action<TwitterCaching, IEnumerable<T>> applying)
            : base(taskname)
        {
            this.SourceCommand = srccmd;
            this.GettingFunc = getting;
            this.TargetCaching = caching;
            this.ApplyingFunc = applying;
        }

        public override void DoTask(object arg)
        {
            IEnumerable<T> result = null;
            try
            {
                try
                {
                    NotifyRefreshStart();

                    if (this.GettingFunc != null)
                        result = this.GettingFunc(this.SourceCommand);
                    else
                        result = new T[0];

                    this.ApplyingFunc(this.TargetCaching, result);
                }
                finally
                {
                    NotifyRefreshEnd();
                }

                NotifyNext(result);
            }
            catch (Exception ex)
            {
                NotifyError(ex);
            }
        }

    }

    //public class DirectMessageUpdateTask
    //    : TwitterUpdateTask<DirectMessage>
    //{
    //    public DirectMessageUpdateTask(TwitterUpdateTask<DirectMessage> task)
    //        : base(task)
    //    { }

    //    protected override void UpdateCachingFunc(IEnumerable<DirectMessage> data)
    //    {
    //        if (this.TargetCaching != null)
    //        {
    //            this.TargetCaching.UpdateDirectMessageDatabase(data, null);
    //        }
    //    }
    //}

    //public class TimelineUpdateTask
    //    : TwitterUpdateTask<Status>
    //{
    //    protected override void UpdateCachingFunc(IEnumerable<Status> data)
    //    {
    //        UpdateCachingFunc(data, TimelineTypes.Home);
    //    }

    //    protected virtual void UpdateCachingFunc(IEnumerable<Status> data, TimelineTypes type)
    //    {
    //        if (this.TargetCaching != null)
    //        {
    //            this.TargetCaching.UpdateDatabase(data, null, type);
    //        }
    //    }
    //}

    //public class MentionsTimelineUpdateTask
    //    : TimelineUpdateTask
    //{
    //    protected override void UpdateCachingFunc(IEnumerable<Status> data)
    //    {
    //        UpdateCachingFunc(data, TimelineTypes.Mentions);
    //    }
    //}

}
